<?php

namespace app\admin\model;

use app\admin\model\article\OnlineVideo;
use app\admin\model\article\Works;
use app\admin\model\course\Course;
use think\Model;


class UserCollection extends Model
{





    // 表名
    protected $name = 'collection';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;


    const TYPE_WORKS = 1;//作品收藏
    const TYPE_ONLINE_VIDEO = 2;//在线视频收藏
    const TYPE_COURSE = 3;//课程

    // 追加属性
    protected $append = [

    ];
    public function course()
    {
        return $this->belongsTo('app\admin\model\course\Course', 'info_id', 'id', [], 'RIGHT')->setEagerlyType(0);
    }
    public function works()
    {
        return $this->belongsTo('app\admin\model\article\Works', 'info_id', 'id', [], 'RIGHT')->setEagerlyType(0);
    }

    public function online()
    {
        return $this->belongsTo('app\admin\model\article\OnlineVideo', 'info_id', 'id', [], 'RIGHT')->setEagerlyType(0);
    }


    public function checkUserCollection($user, $info_id,$type){
        if(!$user){
            return false;
        }
        $check = $this->where("user_id", $user["id"])
            ->where("info_id", $info_id)
            ->where("type", $type)
            ->find();
        if($check){
            return  true;
        }
        return  false;
    }

    /*
     * 收藏课程
     * */
    public function collectionInfo($user,$info_id,$type){
        //先检测是否收藏
        if($this->checkUserCollection($user, $info_id,$type)){
            exception("您已经收藏该内容了");
        }
        $this->save([
            "user_id"=>$user["id"],
            "info_id"=>$info_id,
            "type"=>$type,
        ]);
    }

    /*
     * 取消收藏
     * */

    public function cancelCollectionInfo($user,$info_id,$type){
        //先检测是否收藏
        if(!$this->checkUserCollection($user, $info_id, $type)){
            exception("您还未收藏该内容");
        }
        $this->where([
            "user_id"=>$user["id"],
            "info_id"=>$info_id,
            "type"=>$type,
        ])->delete();
    }

    /*
    * 收藏列表
    * */

    public function collectionInfoList($user,$type){
        switch ($type){
            case self::TYPE_WORKS:
                $worksModel = new Works();
                $list = $this->with("works")->where([
                    "user_id"=>$user["id"],
                    "type"=>$type,
                ])->paginate();
                foreach ($list as $key => $value){
                    $value['info'] = $worksModel->dealInfo($value['works']);
                    unset($value['works']);
                }
                break;
            case self::TYPE_ONLINE_VIDEO:
                $onlineVideoModel = new OnlineVideo();
                $list = $this->with("online")->where([
                    "user_id"=>$user["id"],
                    "type"=>$type,
                ])->paginate();
                foreach ($list as $key => $value){
                    $value['info'] = $onlineVideoModel->dealInfo($value['online']);
                    unset($value['online']);
                }
                break;
            case self::TYPE_COURSE:
                $courseModel = new Course();
                $list = $this->with(["course"=>function($query){
                    $query->withField("id, name, image,price,desc,class_hour");
                }])->where([
                    "user_id"=>$user["id"],
                    "type"=>$type,
                ])->paginate();
                foreach ($list as $key => $value){
                    $value['info'] = $courseModel->dealInfo($value['course']);
                    unset($value['course']);
                }
                break;
        }
        return $list;
    }
}
